﻿Imports System.Data.SqlClient
Imports CodeMatrix.Context
Imports System.Linq

Partial Class Apps_Shares_Uc_staff_list
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        'Hide column checkbox
        Dim btnDelete As Button = CType(gvDisplay.FindControl("btnDelete"), Button)
        If Not btnDelete Is Nothing Then
            btnDelete.Visible = Not String.IsNullOrEmpty(Request.QueryString("del"))
        End If
        ' Set breadcrump
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        If Not Request.QueryString("del") Is Nothing Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 16)
        ElseIf Not Request.QueryString("edit") Is Nothing Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 15)
        Else
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 13)
        End If

        Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable("exec USP_GetDepartmentHirachy", New SqlParameter("@lv", Session("master_department_level")))
    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click

        Try
            'Filter data
            gvDisplay.DataBind()
            gvDisplay.Sort("staff_no", SortDirection.Ascending)
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub


    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Try
            srcSearch.Delete()
            gvDisplay.DataBind()
        Catch ex As SqlException
            GenericHelper.ShowAlert(Me, _
                                    String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, "") _
                                    )
        End Try

    End Sub


    Protected Sub gvDisplaySrc_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles srcSearch.Deleting

        Try
            ' ============================================
            ' Assign parameter to SQL statement to delete
            ' ============================================
            Dim keys As Int32() = CodeMatrix.Utilities.WebUiHelper.GetSelectedCheckBox(Of Int32)(gvDisplay, "staff_code", "icbItem")
            'Dim ctx As DataClassesDataContext = New DataClassesDataContext
            'Try
            '    For Each i In ctx.stp_auth_staffs.Where(Function(f) keys.Contains(f.staff_code))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.master_departments.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.master_departments.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_departments.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_departments.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.group_users.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.group_users.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.staffs.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.staffs.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_main_systems.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_main_systems.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_menu_systems.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_menu_systems.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_sub_menus.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_sub_menus.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.master_types.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.master_types.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_auth_departments.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_auth_departments.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_auth_groups.Where(Function(f) keys.Contains(f.staff_code_create))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_create).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next

            '    For Each i In ctx.stp_auth_groups.Where(Function(f) keys.Contains(f.staff_code_update))
            '        Dim staff_firstname = ctx.staffs.SingleOrDefault(Function(f) f.staff_code = i.staff_code_update).staff_firstname
            '        Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, staff_firstname))
            '    Next
            'Catch ex As Exception
            '    e.Cancel = True
            '    GenericHelper.ShowAlert(Me, ex.Message)
            'End Try

            If keys.Length > 0 Then
                e.Command.Parameters("@cid").Value = CodeMatrix.Utilities.CodeHelper.CreateSeriesOfParameters(keys)
                Dim value As New OrderedDictionary()
                value.Add(e.Command.Parameters("@pid").Value, e.Command.Parameters("@cid").Value)
                GenericHelper.GenerateScript(GenerateScriptLogType.Delete, LogMenuType.staff, value)
            Else
                e.Cancel = True
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try


    End Sub



    Protected Sub TemplatePagerField_OnPagerCommand(ByVal sender As Object, ByVal e As DataPagerCommandEventArgs)

        'Just to decive TemplatePagerField

    End Sub

    Protected Sub srcSearch_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles srcSearch.Selecting

        Try
            If Not IsPostBack Then
                e.Cancel = True
            Else
                e.Command.Parameters("@department_code").Value = ddlDependDepartment.SelectedValue
                e.Command.Parameters("@group_user_code").Value = ddlUserGroup.SelectedValue
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub srcSearch_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles srcSearch.Selected

        Try
            If e.AffectedRows > 0 Then
                ltrCountView.Text = String.Format("จำนวน {0} เร็คคอร์ด", e.AffectedRows)
            Else
                ltrCountView.Text = String.Empty
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub ddlDependDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDependDepartment.SelectedIndexChanged

        Try
            ddlUserGroup.DataSource = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            "select group_user_code = 0 , group_user_name = 'กลุ่มผู้ใช้งาน' union all select group_user_code , group_user_name from group_user where department_code = @department_code and group_user_status = 1 and group_user_code > 0", _
            New SqlParameter() {New SqlParameter("@department_code", ddlDependDepartment.SelectedValue)} _
            )
            ddlUserGroup.DataBind()
            ddlUserGroup.SelectedValue = 0
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub

    Protected Sub gvDisplay_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles gvDisplay.Sorting

        Try
            If (String.IsNullOrEmpty(e.SortExpression)) Then
                Dim oldSortCol As Image = gvDisplay.FindControl(gvDisplay.SortExpression)
                If Not oldSortCol Is Nothing Then oldSortCol.Visible = False
            Else
                Dim oldSortCol As Image = gvDisplay.FindControl(gvDisplay.SortExpression)
                Dim newSortCol As Image = gvDisplay.FindControl(e.SortExpression)

                If (Not oldSortCol Is Nothing) Then oldSortCol.Visible = False
                If (Not newSortCol Is Nothing) Then
                    newSortCol.Visible = True
                    If e.SortDirection = SortDirection.Ascending Then
                        newSortCol.ImageUrl = "../images/btn_down.png"
                    Else
                        newSortCol.ImageUrl = "../images/btn_up.png"
                    End If
                End If
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub
End Class
